package algorithm_demo.leetcode;

/**
 * 19. 删除链表的倒数第 N 个结点
 * https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
 * @author Api
 * @date 2023/4/29 16:24
 */
public class Problem_019_RemoveNthNodeFromEndOfList {

    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode cur = head;
        ListNode pre = null;
        boolean enough = false;
        while(cur != null){
            n--;
            if (n <= 0){
                if (n == 0){
                    enough = true;
                }else if( n == -1){
                    pre = head;
                }else{
                    pre = pre.next;
                }
            }
            cur = cur.next;
        }
        if (!enough){
            return head;
        }
        if (pre == null){
            return head.next;
        }
        pre.next = pre.next.next;
        return head;
    }
}
